﻿using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Validation.sp2010.Classes;

namespace Validation.sp2010.ControlTemplates.Validation.sp2010
{
    public partial class GetUser : UserControl
    {
        const string state_withSecondaryValue = "WithSecondaryValue";
        #region public properties
        public void LoadData(List<SPField> fields, List<CoreProperty> userProfileProeprties)
        {
            Columns.DataSource = fields;
            Columns.DataBind();

            ColumnsUserProfile.DataSource = fields;
            ColumnsUserProfile.DataBind();

            UserProfilëProperty.DataSource = userProfileProeprties;
            UserProfilëProperty.DataBind();
        }
        public void LoadData(List<SPField> fields, List<CoreProperty> userProfileProeprties,
            bool multiSelect,bool enableGroup,bool withSecondaryValue)
        {
            PeopleUser.MultiSelect = multiSelect;
            if (!enableGroup)
                PeopleUser.SelectionSet = "User";

            //mask if mail
            if (!withSecondaryValue)
            {
                LiteralColumn.Visible = false;
                LiteralUserProfile.Visible = false;
                PeopleColumnUserEmpty.Visible = false;
                PeopleUserProfileEmpty.Visible = false;
                ViewState[state_withSecondaryValue] = withSecondaryValue;
            }

            Columns.DataSource = fields;
            Columns.DataBind();

            ColumnsUserProfile.DataSource = fields;
            ColumnsUserProfile.DataBind();

            UserProfilëProperty.DataSource = userProfileProeprties;
            UserProfilëProperty.DataBind();
        }

        public void LoadData(List<SPField> fields, List<CoreProperty> userProfileProeprties,StepPrincipal principal)
        {
            if (principal != null)
            {
                TypeUserValidation.SelectedIndex = (int)principal.TypeUser - 1;

                switch (principal.TypeUser)
                {
                    case TypeUser.StaticUser:
                        {
                            PeopleUser.CommaSeparatedAccounts = principal.StaticUser[0];
                            break;
                        }
                    case TypeUser.PropertyUser:
                        {
                            Columns.Items.FindByValue(principal.ColumnUser).Selected = true;
                            PeopleColumnUserEmpty.CommaSeparatedAccounts = principal.SecondaryValidator;
                            TypeUserv1.Style.Add(HtmlTextWriterStyle.Display, "none");
                            TypeUserv2.Style[HtmlTextWriterStyle.Display] = "";

                            break;
                        }
                    case TypeUser.UserProfileOfPropertyUser:
                        {
                            ColumnsUserProfile.Items.FindByValue(principal.ColumnUser).Selected = true;
                            PeopleUserProfileEmpty.CommaSeparatedAccounts = principal.SecondaryValidator;
                            UserProfilëProperty.Items.FindByValue(principal.UserProfilePropertyName).Selected = true;

                            TypeUserv1.Style.Add(HtmlTextWriterStyle.Display, "none");

                            TypeUserv3.Style[HtmlTextWriterStyle.Display] = "";
                            break;
                        }

                }
            }


            Columns.DataSource = fields;
            Columns.DataBind();

            ColumnsUserProfile.DataSource = fields;
            ColumnsUserProfile.DataBind();

            UserProfilëProperty.DataSource = userProfileProeprties;
            UserProfilëProperty.DataBind();

            this.DataBind();
        }
        

        public StepPrincipal GetPrincipal()
        {
            if (TypeUserValidation.SelectedIndex == 0 && PeopleUser.Accounts.Count == 0)
            {
                ErrorActor.Visible = true;
                return null;
            }
            else
                ErrorActor.Visible = false;

            if (ViewState[state_withSecondaryValue] != null && (bool)ViewState[state_withSecondaryValue])
            {
                if (TypeUserValidation.SelectedIndex == 1 && PeopleColumnUserEmpty.Accounts.Count == 0)
                {
                    ErrorActorColumn.Visible = true;
                    return null;
                }
                else
                    ErrorActorColumn.Visible = false;


                if (TypeUserValidation.SelectedIndex == 2 && PeopleUserProfileEmpty.Accounts.Count == 0)
                {
                    ErrorActorProfile.Visible = true;
                    return null;
                }
                else
                    ErrorActorProfile.Visible = false; 
            }

            StepPrincipal principal = new StepPrincipal();
            principal.ColumnUser = (TypeUserValidation.SelectedIndex == 1) ? Columns.SelectedValue : ColumnsUserProfile.SelectedValue;
            principal.DisplayColumnUser = (TypeUserValidation.SelectedIndex == 1) ? Columns.SelectedItem.Text : ColumnsUserProfile.SelectedItem.Text;
            
            principal.UserProfilePropertyName = UserProfilëProperty.SelectedValue;
            if(UserProfilëProperty.SelectedItem!=null)
            principal.DisplayUserProfilePropertyName = UserProfilëProperty.SelectedItem.Text;

            principal.TypeUser = (TypeUser)(int.Parse(TypeUserValidation.SelectedValue));

            if (ViewState[state_withSecondaryValue] != null && (bool)ViewState[state_withSecondaryValue])
            {
                if (TypeUserValidation.SelectedIndex == 1)
                {
                    principal.SecondaryValidator = PeopleColumnUserEmpty.Accounts[0].ToString();
                    principal.DisplaySecondaryValidator = ((PickerEntity)PeopleColumnUserEmpty.Entities[0]).Description;
                }
                if (TypeUserValidation.SelectedIndex == 2)
                {
                    principal.SecondaryValidator = PeopleUserProfileEmpty.Accounts[0].ToString();
                    principal.DisplaySecondaryValidator = ((PickerEntity)PeopleUserProfileEmpty.Entities[0]).Description;

                }
            }

            principal.StaticUser=new List<string>();

            foreach(var account in PeopleUser.Accounts)
            {
                principal.StaticUser.Add(account.ToString());
            }

            return principal;
        }
        
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        
    }
}
